Slide 13
- list can store different data types
- data frames can store different values with different types
- class()
- is.character()
Slide 14
[] = always returns object of same class
[[]] = can extract one element from list or data
frame
list_a <- list(a = 2, b = "hallo")
list_a["a"]
list_a[["a"]]
list_a$a
[ selects sub-lists. It always returns a list; if you
use it with a single positive integer, it returns a list of length
one.
[[ selects an element within a list.
$ is a convenient shorthand: x$y is
equivalent to x[["y"]].
- Use
drop = FALSE if you are subsetting a matrix, array,
or data frame and you want to preserve the original dimensions. You
should almost always use it when subsetting inside a function.
Slide 15
if() works with scalars; ifelse() works
with vectors.
- when
x is TRUE, y will be
3;
- when
FALSE, y will be
NULL;
- when
NA the if statement will throw an
error.
- You can rewrite any
for loop to use while
instead, and you can rewrite any while loop to use
repeat, but the converses are not true. That means
while is more flexible than for, and
repeat is more flexible than while.
if("2") 3
if(2) 3
for(i in 1:3){
print(i)
}
i <- 1
while(i < 4){
print(i)
i <- i + 1
}
i <- 1
repeat{
print(i)
i <- i + 1
if(i > 3) break
}